<?php
/**
 * +----------------------------------------------------------------------
 * 用户管理
 * +----------------------------------------------------------------------
 * Author: ZF <ZFajpst_2008@126.com> 2012-10-23 16:55:00
 * +----------------------------------------------------------------------
 */
 
	class User_model extends CI_Model {
	
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
	
		/**
		 * +---------------------
		 * 用户管理
		 * +---------------------
		 */
		public function index($id, $num, $offset){
			$this->db->select('oa_depart.name, oa_user.id, oa_user.username, oa_user.realname, oa_user.position, oa_user.addtime');
			$this->db->from('oa_user');
			$this->db->join('oa_depart', 'oa_user.departid = oa_depart.id');
			$this->db->order_by('oa_user.id desc');
			$id ? $this->db->where('oa_user.departid', $id) : null;
			
			$offset = ($offset - 1) * $num < 0 ? 0 : ($offset - 1) * $num;
			$query = $this->db->get('', $num, $offset);
			
			return $query->result_array();
		}
	
		/**
		 * +---------------------
		 * 用户添加
		 * +---------------------
		 */
		public function add(){
			$query = $this->db->get_where('oa_user', array('username' => $this->input->post('username')));
			if($query->num_rows()){
				return false;
			}
			
			$data = array(
				'departid' => $this->input->post('departid'),
				'username' => $this->input->post('username'),
				'userpwd' => md5($this->input->post('userpwd')),
				'islock' => $this->input->post('islock'),
				'realname' => $this->input->post('realname'),
				'position' => $this->input->post('position'),
				'sex' => $this->input->post('sex'),
				'worktel' => $this->input->post('worktel'),
				'lognum' => 0,
				'logtime' => time(),
				'addtime' => time(),
				'notes' => $this->input->post('notes')
			);
			
			return $this->db->insert('oa_user', $data);
		}
		
		/**
		 * +---------------------
		 * 用户修改
		 * +---------------------
		 */
		public function edit($id, $action = ''){
			// 显示
			if($action != 'do'){
				$query = $this->db->get_where('oa_user', array('id' => $id));
				return $query->row_array();
			}
			
			// 执行
			$userpwd = $this->input->post('userpwd');
			if(!empty($userpwd)){
				$userpwd = md5($userpwd);
			}else{
				$userpwd = $this->input->post('userpwdo');
			}
			
			$data = array(
				'departid' => $this->input->post('departid'),
				'username' => $this->input->post('username'),
				'userpwd' => $userpwd,
				'islock' => $this->input->post('islock'),
				'realname' => $this->input->post('realname'),
				'position' => $this->input->post('position'),
				'sex' => $this->input->post('sex'),
				'worktel' => $this->input->post('worktel'),
				'lognum' => 0,
				'logtime' => time(),
				'addtime' => time(),
				'notes' => $this->input->post('notes')
			);
			
			return $this->db->update('oa_user', $data, array('id' => $id));
		}
		
		/**
		 * +---------------------
		 * 用户删除
		 * +---------------------
		 */
		public function del($id){
			if(!$id){
				$this->db->where_in('id', $this->input->post('did'));
				return $this->db->delete('oa_user');
			}
			return $this->db->delete('oa_user', array('id' => $id));
		}
		
		/**
		 * +---------------------
		 * 获取总记录数
		 * +---------------------
		 */
		public function getcount($id){
			$id ? $this->db->where('departid', $id) : null;
			$this->db->from('oa_user');
			return $this->db->count_all_results();
		}
	
	}